package org.xmppbot.net;

/**
 * Hides net package implementation internals.
 *
 * @author Bulat Nigmatullin
 */
public abstract class Accessor {

    private static volatile Accessor INSTANCE;

    /**
     * Accessor can be accessed from the package only.
     *
     * @return Accessor implementation
     */
    static Accessor getInstance() {
        Accessor a = INSTANCE;
        if (a == null) {
            try {
                Class.forName("org.xmppbot.net.nio.BotSocketAcceptor");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return a;
    }

    /**
     * Implementation sets Accessor instance.
     * 
     * @param accessor
     */
    public static void setInstance(Accessor accessor) {
        if (INSTANCE != null)
            throw new IllegalStateException("Accessor instance already initialized");
        INSTANCE = accessor;
    }

    /**
     * Create new NIO implementation connection strategy.
     *
     * @return nio connection strategy
     */
    protected abstract ConnectionStrategy newNIOConnectionStrategy();

}
